﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CUREEntity;
using CUREDataAccessLayers;
using System.Data;
namespace CUREBusinessLogicLayer
{
    public class MenuBll
    {
        MenuDal menuDal = new MenuDal();
        /// <summary>
        /// 菜单分页方法
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">每页显示的数据条数</param>
        /// <param name="totalRecordCount">总记录数</param>
        /// <param name="totalPageCount">总页数</param>
        /// <returns></returns>
        public DataSet GetMenuData(IDictionary<string, object> where, int pageIndex, int pageSize, out int totalRecordCount, out int totalPageCount)
        {
            DataSet ds = new DataSet();
            try
            {
                ds = menuDal.GetMenuData(where, pageIndex, pageSize, out totalRecordCount, out totalPageCount);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return ds;
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns>返回true或false true 添加成功；false 添加失败</returns>
        public bool InsertMenu(int id, string name, string menuSort)
        {
            bool addSuccess = false;
            try
            {
                if (!string.IsNullOrEmpty(name))
                {
                    int result = menuDal.InsertMenu(id, name,menuSort);
                    if (result > 0)
                    {
                        addSuccess = true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return addSuccess;
        }
        /// <summary>
        /// 根据菜单名称查询数据
        /// </summary>
        /// <param name="name">菜单名称</param>
        /// <returns>返回true或false true 存在；false不存在</returns>
        public bool SelectMenuByName(string name)
        {
            bool isExist = false;
            DataTable dt = menuDal.GetMenuByName(name);
            if (dt != null && dt.Rows.Count > 0)
            {
                isExist = true;
            }
            return isExist;
        }
        /// <summary>
        /// 获取最大的id
        /// </summary>
        /// <returns></returns>
        public int GetMaxId()
        {
            int maxid = 0;
            decimal id = menuDal.GetMaxId();
            if (id.ToString() != null && id.ToString() != "")
            {
                maxid = Convert.ToInt32(id);
            }
            return maxid;
        }
        /// <summary>
        /// 根据id获取菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetMenuById(string id)
        {
            DataTable dt = new DataTable();
            DataTable table = menuDal.GetMenuInfoById(id);
            if (table != null && table.Rows.Count > 0)
            {
                dt = table;
            }
            return dt;
        }
        /// <summary>
        /// 根据菜单数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns>返回true或false true更新成功；false更新失败</returns>
        public bool UpdateMenu(string id, string name,string menuSort)
        {
            bool updateSuccess = false;
            if (!string.IsNullOrEmpty(name)&&!string.IsNullOrEmpty(id))
            {
                int result = menuDal.UpdateMenu(id, name,menuSort);
                if (result > 0)
                {
                    updateSuccess = true;
                }
            }
            return updateSuccess;
        }
        /// <summary>
        /// 根据id和名称查询数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns>返回true或false true存在；false 不存在</returns>
        public bool SelectMenuByIdAndName(string id,string name)
        {
            bool isExist = false;
            DataTable dt = menuDal.GetMenuByIdAndName(id,name);
            if (dt != null && dt.Rows.Count > 0)
            {
                isExist = true;
            }
            return isExist;
        }
        /// <summary>
        /// 根据id删除菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回true或false true删除成功；false删除失败</returns>
        public bool DelMenuById(string id)
        {
            bool delSuccess = false;
            if (!string.IsNullOrEmpty(id))
            {
                int result = menuDal.DeleteMenu(id);
                if (result > 0)
                {
                    delSuccess = true;
                }
            }
            return delSuccess;
        }
    }
}
